package org.springblade.modules.admin.controller;

import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.dto.BuildingElevatorDTO;
import org.springblade.modules.admin.service.IBuildingElevatorVisitorService;
import org.springblade.modules.admin.utils.GzipUtils;
import org.springblade.modules.admin.vo.BuildingAlreadyVO;
import org.springblade.modules.admin.vo.BuildingElevatorVO;
import org.springblade.modules.admin.vo.BuildingElevatorVisitorVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 游客报障设置 维修设置 Controller
 *
 * @author: Sharry
 * @createTime: 2023/4/10 10:44
 * @version: Version-1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/visitor/config")
@Api(tags = "维修设置-游客报障", value = "repair/visitor/config")
public class RepairConfigController {

	private final IBuildingElevatorVisitorService bevService;

	/**
	 * 添加项目电梯-提交项目电梯全量更新
	 */
	@PostMapping("add/list/submit")
	@Transactional(rollbackFor = Exception.class)
	@ApiOperation("提交项目电梯")
	@ApiOperationSupport(order = 10)
	public R<Boolean> addElevators(@RequestBody List<BuildingElevatorVO> buildingElevatorList) {
		// 先删除当前租户全部，再全量更新
		bevService.deleteALL(AuthUtil.getTenantId());
		List<BuildingElevatorDTO> buildingElevatorDTOs = bevService.getBuildingElevatorSelected(buildingElevatorList);
		return R.data(bevService.addElevators(buildingElevatorDTOs));
	}


	/**
	 * 删除游客可报障电梯
	 * 根据项目批量删除可报障电梯
	 */
	@PostMapping("building/delete")
	@ApiOperation("根据项目批量删除可报障电梯")
	@ApiOperationSupport(order = 20)
	public R<Boolean> deleteElevatorsByBuildingId(@RequestBody Map<String, Long> buildingId) {
		Long id = buildingId.get("buildingId");
		return R.data(bevService.removeByBuildingId(id));
	}

	/**
	 * 删除游客可报障电梯
	 * 根据自选批量删除可报障电梯
	 */
	@PostMapping("batch/delete")
	@ApiOperation("根据自选批量删除可报障电梯")
	@ApiOperationSupport(order = 30)
	public R<Boolean> deleteElevatorsBatch(@RequestBody List<Long> ids) {
		return R.data(bevService.removeByIds(ids));
	}

	/**
	 * 根据电梯 id 列表， 删除可报障电梯
	 */
	@PostMapping("batch/delete/list")
	@ApiOperation("根据自选批量删除可报障电梯")
	@ApiOperationSupport(order = 31)
	public R<Boolean> deleteElevatorsByEId(@RequestBody List<Long> elevatorIds) {
		return R.data(bevService.removeByElevatorIds(elevatorIds));
	}

	/**
	 * 添加项目电梯
	 * 获取项目-电梯列表，可模糊查询项目名称
	 */
	@PostMapping("add/list/buildings")
	@ApiOperation("获取项目-电梯列表")
	@ApiOperationSupport(order = 40)
	public R<List<BuildingElevatorVO>> getBuildingListByName(@RequestBody Map<String, String> param) {
		List<BuildingElevatorVO> buildingElevatorList = bevService.getListByBuildingName(param);
		return R.data(buildingElevatorList);
	}


	/**
	 * 添加项目电梯
	 * 获取项目-电梯列表，可模糊查询项目名称
	 */
	@PostMapping("add/list/buildings/v2")
	@ApiOperation("获取项目-电梯列表")
	@ApiOperationSupport(order = 40)
	public R<String> getBuildingListByNameV2(@RequestBody Map<String, String> param) throws IOException {
		List<BuildingElevatorVO> buildingElevatorList = bevService.getListByBuildingName(param);
		String gzip = GzipUtils.gzip(JSON.toJSONString(buildingElevatorList));
		final String ungzipGBK = GzipUtils.ungzipGBK(gzip);
		return R.data(gzip);
	}


	/**
	 * 查询即将报障电梯
	 * 查询选中即将报障的电梯
	 */
	@PostMapping("add/list/buildings/select")
	@ApiOperation("获取项目-准备报障电梯列表")
	@ApiOperationSupport(order = 50)
	public R<List<BuildingElevatorDTO>> getBuildingElevatorSelected(@RequestBody List<BuildingElevatorVO> buildingElevatorList) {
		List<BuildingElevatorDTO> beDTOList = bevService.getBuildingElevatorSelected(buildingElevatorList);
		return R.data(beDTOList);
	}

	/**
	 * 查询已设置可报障电梯
	 * 根据项目查询可报障电梯
	 */
	@GetMapping("visitors/list/elevators")
	@ApiOperation("获取项目-可报障电梯列表")
	@ApiOperationSupport(order = 60)
	public R<List<BuildingElevatorVisitorVO>> getVisitorsElevators(Long buildingId) {
		List<BuildingElevatorVisitorVO> bevList = bevService.getVisitorsElevatorsById(buildingId);
		return R.data(bevList);
	}

	/**
	 * 已设置可报障电梯统计
	 */
	@GetMapping("/already")
	@ApiOperation("获取项目-已设置可报障电梯数量")
	@ApiOperationSupport(order = 70)
	public R<List<BuildingAlreadyVO>> getAlready() {
		return R.data(bevService.getAlready());
	}

	/**
	 * 获取全部已报障电梯树
	 */
	@GetMapping("/all")
	@ApiOperation("获取全部已设置游客报障电梯")
	@ApiOperationSupport(order = 71)
	public R<List<BuildingElevatorVO>> getAll() {
		return R.data(bevService.getTree());
	}

}
